package com.code.utils;

import org.apache.commons.text.StringSubstitutor;

import java.util.Map;

public class GeneratorUtil {

    public static String getTemplateResult(String source, Map<String, String> params) throws Exception {
        StringSubstitutor sub = new StringSubstitutor(params);
        String result = "";
        try {
            result = sub.replace(source);
        } catch (Exception e) {
            throw new Exception("参数 " + params.toString() + " 与定义的模板【" + source + "】不匹配", e);
        }
        return result;
    }

    public static String getStrWithoutPrefix(String source) {
        int indexOfNum = 0;
        for (; indexOfNum < source.length(); indexOfNum++) {
            if (!Character.isDigit(source.charAt(indexOfNum))) {
                break;
            }
        }
        if (indexOfNum != 0) {
            // 数字开头
            source = source.substring(indexOfNum);
            while (source.startsWith("_")) {
                source = source.substring(1);
                source = getStrWithoutPrefix(source);
            }
        }
        return source;
    }

}
